package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class GameCircleAdapter extends RecyclerView.Adapter<GameCircleAdapter.GameViewHolder> {

    // 数据列表
    private List<Game> mGameList;

    // 点击事件监听器
    private OnItemClickListener mClickListener;

    // 长按事件监听器
    private OnItemLongClickListener mLongClickListener;

    // 构造方法，接收数据和监听器
    public GameCircleAdapter(List<Game> gameList, OnItemClickListener clickListener,
                             OnItemLongClickListener longClickListener) {
        mGameList = gameList;
        mClickListener = clickListener;
        mLongClickListener = longClickListener;
    }

    // 创建ViewHolder
    @NonNull
    @Override
    public GameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_game_circle, parent, false);
        return new GameViewHolder(view);
    }

    // 绑定数据到ViewHolder
    @Override
    public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
        Game game = mGameList.get(position);
        holder.gameName.setText(game.getName());
        holder.gameDescription.setText(game.getDescription());
        holder.gameIcon.setImageResource(game.getIconResId());

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mClickListener != null) {
                mClickListener.onItemClick(game);
            }
        });

        // 设置长按事件
        holder.itemView.setOnLongClickListener(v -> {
            if (mLongClickListener != null) {
                mLongClickListener.onItemLongClick(game, position);
                return true; // 消费事件，避免触发点击事件
            }
            return false;
        });
    }

    // 获取列表项数量
    @Override
    public int getItemCount() {
        return mGameList == null ? 0 : mGameList.size();
    }

    // 更新数据
    public void updateData(List<Game> newList) {
        mGameList = newList;
        notifyDataSetChanged(); // 通知列表刷新
    }

    // ViewHolder类，缓存列表项控件
    public static class GameViewHolder extends RecyclerView.ViewHolder {
        ImageView gameIcon;
        TextView gameName;
        TextView gameDescription;

        public GameViewHolder(@NonNull View itemView) {
            super(itemView);
            gameIcon = itemView.findViewById(R.id.iv_game_icon);
            gameName = itemView.findViewById(R.id.tv_game_name);
            gameDescription = itemView.findViewById(R.id.tv_game_description);
        }
    }

    // 点击事件接口
    public interface OnItemClickListener {
        void onItemClick(Game game);
    }

    // 长按事件接口
    public interface OnItemLongClickListener {
        void onItemLongClick(Game game, int position);
    }
}
